//
//  SignatureDateBaseViewController.m
//  Today
//
//  Created by dllo on 15/5/30.
//  Copyright (c) 2015年 fanfan. All rights reserved.
//

#import "SignatureDateBaseViewController.h"

@interface SignatureDateBaseViewController ()

@end

@implementation SignatureDateBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    [self.view addSubview:tableview];
    sign = [[SignatureData alloc] init];
    self.array = [NSMutableArray arrayWithArray:[sign selectSignaturetable]];
    self.array1 = [NSMutableArray array];
    sign = [[SignatureData alloc]init];
    self.array1 = [NSMutableArray arrayWithArray:[sign selectSignaturePic]];
   // [SignatureData openDataBase];
    //[SignatureData createTable];
    tableview.delegate = self;
    tableview.dataSource = self;
    self.array = [NSMutableArray arrayWithArray:[sign selectSignaturetable]];
    self.array1 = [NSMutableArray arrayWithArray:[sign selectSignaturePic]];
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
#pragma mark 启动编辑
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [tableview setEditing:editing animated:animated];
}
#pragma mark 允许编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
#pragma _mark 编辑方式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
#pragma mark 确认编辑
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSString *str = [self.array objectAtIndex:indexPath.row];
        
        sign = [[SignatureData alloc]init];
        [sign deleteTable:str];
        //[SignatureData deleteTable:str];
        [self.array removeObjectAtIndex:indexPath.row];
        [self.array1 removeObjectAtIndex:indexPath.row];
        NSString *str1 = [self.array1 objectAtIndex:indexPath.row];
        
        sign = [[SignatureData alloc]init];
        [sign deleteTable:str1];
        //[SignatureData deleteTable:str1];
        [tableview deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation: UITableViewRowAnimationRight];
    }
    //            更新数据
    self.array = [NSMutableArray arrayWithArray:[sign selectSignaturetable]];
    self.array1 = [NSMutableArray arrayWithArray:[sign selectSignaturePic]];
    [tableview reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"cell";
    DataTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (cell == nil) {
        cell = [[DataTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];
    }
    cell.title.text  = [self.array objectAtIndex:indexPath.row];
    NSURL *url = [NSURL URLWithString:[self.array1 objectAtIndex:indexPath.row]];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *image = [UIImage imageWithData:data];
    cell.imagePic.image = image;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 180;
}- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
